package com.qf.dao;

import com.qf.entity.User;
import com.qf.utils.DBHelper;

import java.sql.ResultSet;

public class UserDao {
    /**
     *作用：用于查询数据库中 是否有该用户
     */
    public User queryUser(String username) {
        // 【1】查询方法 - 编写一个查询的sql语句
        String sql = "select * from user where F_username='"+username+"'";
        DBHelper dbHelper = new DBHelper(sql); //实例化数据库访问的帮助类
        // 【2】实例化用户类
        User user = new User();
        // 【3】开始查询数据
        try {
            ResultSet resultSet = dbHelper.pst.executeQuery();
            while (resultSet.next()){
                user.setFId(resultSet.getInt("F_id"));
                user.setFUsername(resultSet.getString("F_username"));
                user.setFPassword(resultSet.getString("F_password"));
                user.setFCreatetime(resultSet.getString("F_createtime"));
                user.setFHeadicon(resultSet.getString("F_headicon"));
                user.setFNickname(resultSet.getString("F_nickname"));
                user.setFMobilephone(resultSet.getString("F_mobilephone"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            dbHelper.close();
        }
        return user;
    }

    // 用户的注册
    public void insertUser(User user) {
        String sql = "insert into user(F_username,F_password,F_createtime,F_headicon,F_nickname,F_mobilephone) value (?,?,?,?,?,?)";
        DBHelper dbHelper = new DBHelper(sql);
        try {
            dbHelper.pst.setString(1,user.getFUsername());
            dbHelper.pst.setString(2,user.getFPassword());
            dbHelper.pst.setString(3,user.getFCreatetime());
            dbHelper.pst.setString(4,user.getFHeadicon());
            dbHelper.pst.setString(5,user.getFNickname());
            dbHelper.pst.setString(6,user.getFMobilephone());
        //     执行sql语句
            dbHelper.pst.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            dbHelper.close();
        }


    }
}
